package com.jiuyun.auth.dao.impl;

import com.jiuyun.auth.dao.RoleDao;
import com.jiuyun.auth.entity.Role;
import com.jiuyun.auth.entity.User;
import com.jiuyun.auth.util.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
   QueryRunner runner = new QueryRunner();
    @Override
    public List Rolefind() throws SQLException {
        String sql = "select * from role";
        Connection conn = AliPool.getConn();
        List list  =runner.query(conn,sql,new BeanListHandler<Role>(Role.class));
        conn.close();
        return list;
    }

    @Override
    public int delRoles(int uid) throws SQLException {
        String sql = "DELETE FROM user2role where userid =?";
        Connection conn = AliPool.getConn();
        int ok = runner.update(conn,sql,uid);
        return ok;
    }

    @Override
    public int addRole(int userId, int roleId) throws SQLException {
        String sql = "INSERT INTO user2role(userid,roleid)values(?,?);";
        Connection conn = AliPool.getConn();
        int ok = runner.update(conn,sql,userId,roleId);
        conn.close();
        return ok;
    }
}
